<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: CategoryListener.cfc 1114 2007-02-27 02:39:09Z mpwoodward $

Notes:
This listener is autowired by ColdSpring.
--->
<cfcomponent
	displayname="CategoryListener"
	extends="MachII.framework.Listener"
	output="false"
	hint="CategoryListener for MachBlog.">

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="configure" access="public" output="false" returntype="void" 
		hint="Configures this listener as part of the Mach-II framework.">
		<!--- Does nothing. --->
	</cffunction>

	<cffunction name="setCategoryService" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="categoryService" type="org.machblog.category.CategoryService" required="true" />
		<cfset variables.categoryService = arguments.categoryService />
	</cffunction>	
	<cffunction name="getCategoryService" access="private" output="false" returntype="org.machblog.category.CategoryService">
		<cfreturn variables.categoryService />
	</cffunction>
	
	<cffunction name="setUserFacade" access="public" output="false" returntype="void">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>
	<cffunction name="getUserFacade" access="public" output="false" returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - GATEWAY RELATED
	--->
	<cffunction name="getAllCategories" access="public" output="false" returntype="query" 
			hint="Returns a query object containing all categories (active and inactive)">
		<cfreturn getCategoryService().getCategories(includeInactive = true) />
	</cffunction>
	
	<cffunction name="getActiveCategories" access="public" output="false" returntype="query" 
			hint="Returns a query object containing active categories">
		
		<cfreturn getCategoryService().getCategories(includeInactive = false) />
	</cffunction>
	
	<cffunction name="getAllCategoriesAsArray" access="public" output="false" returntype="array" 
			hint="Returns an array containing active categories and entry counts by category">
		<cfreturn getCategoryService().getCategoriesAsArray(includeInactive = true) />
	</cffunction>
	
	<cffunction name="getActiveCategoriesAsArray" access="public" output="false" returntype="array" 
			hint="Returns an array containing active categories and entry counts by category">
		<cfreturn getCategoryService().getCategoriesAsArray(includeInactive = false) />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - DAO RELATED
	--->
	<cffunction name="getCategory" access="public" output="false" returntype="org.machblog.category.Category"
		hint="Retrieves a category by ID">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var categoryId = CreateUUID() />
		<cfset var results =  "" />
		
		<cfif arguments.event.isArgDefined("categoryId")>
			<cfset categoryId = arguments.event.getArg("categoryId") />
		</cfif>
		
		<cfreturn getCategoryService().getCategory(categoryId) />
	</cffunction>

	<cffunction name="processCategoryForm" access="public" returntype="void" output="false"
		hint="Processes the category form and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var category = arguments.event.getArg("category") />
		<cfset var success = true />
				
		<cfif arguments.event.isArgDefined("isActive")>
			<cfset category.getAudit().setIsActive(arguments.event.getArg("isActive")) />
		</cfif>
		
		<cfif category.getCategoryId() EQ "">
			<cfset category.setCategoryId(CreateUUID()) />
			<cfset category.getAudit().setCreatedById(getUserFacade().getUser().getUserId()) />
			<cfset category.getAudit().setDtCreated(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset category.getAudit().setIpCreated(CGI.REMOTE_ADDR) />
			
			<cftry>
				<cfset getCategoryService().createCategory(category) />
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("categorycreatesuccessmessage")) />
				<cfcatch type="any">
					<cfset success = false />
					
					<cfif CFCATCH.Message EQ "duplicate category">
						<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("duplicatecategory")) />
					<cfelse>
						<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("categorycreatefailedmessage")) />
					</cfif>
				</cfcatch>
			</cftry>
		<cfelse>
			<cfset category.getAudit().setModifiedById(getUserFacade().getUser().getUserId()) />
			<cfset category.getAudit().setDtModified(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset category.getAudit().setIpModified(CGI.REMOTE_ADDR) />
			
			<cftry>
				<cfset getCategoryService().updateCategory(category) />
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("categoryupdatesuccessmessage")) />
				<cfcatch type="any">
					<cfset success = false />
					
					<cfif CFCATCH.Message EQ "duplicate category">
						<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("duplicatecategory")) />
					<cfelse>
						<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("categoryupdatefailedmessage")) />
					</cfif>
				</cfcatch>
			</cftry>
		</cfif>
		
		<cfif success>
			<cfset announceEvent("showCategoryForm", arguments.event.getArgs()) />
		<cfelse>
			<cfset category.setCategoryId("") />
			<cfset announceEvent("showCategoryForm_error", arguments.event.getArgs()) />
		</cfif>
	</cffunction>
	
	<cffunction name="deleteCategory" access="public" returntype="void" output="false"
		hint="Deletes a category and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var category = arguments.event.getArg("category") />

		<cftry>
			<cfset getCategoryService().deleteCategory(category) />
			<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("categorydeletesuccessmessage")) />
			<cfcatch type="any">
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("categorydeletefailedmessage")) />
			</cfcatch>
		</cftry>
		
		<cfset announceEvent("showCategoryAdmin", arguments.event.getArgs()) />
	</cffunction>

</cfcomponent>